home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (c) 1985 Regents of the University of California.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms are permitted
- * provided that: (1) source distributions retain this entire copyright
- * notice and comment, and (2) distributions including binaries display
- * the following acknowledgement: ``This product includes software
- * developed by the University of California, Berkeley and its contributors''
- * in the documentation or other materials provided with the distribution
- * and in all advertising materials mentioning features or use of this
- * software. Neither the name of the University nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
- */
-
- #if defined(LIBC_SCCS) && !defined(lint)
- static char sccsid[] = "@(#)getusershell.c 5.6 (Berkeley) 6/1/90";
-
- #endif /* LIBC_SCCS and not lint */
-
- #ifndef AMIGA
- #include "../src/config.h"
- #else
- #include "config.h"
- #endif
-
- #include <sys/types.h>
- #include <sys/param.h>
- #include <sys/file.h>
- #include <sys/stat.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <stdio.h>
-
- #ifdef AMIGA
- #define SHELLS "AmiTCP:db/shells"
- #else
- #define SHELLS "/etc/shells"
- #endif
-
- /*
- * Do not add local shells here. They should be added in /etc/shells
- */
- static char *okshells[] =
- #ifndef AMIGA
- {"/bin/sh", "/bin/csh", 0};
- #else
- {"shell", "cli", 0};
- #endif
-
- static char **shells,
- *strings;
- static char **curshell = NULL;
- static char **initshells();
-
- /*
- * Get a list of shells from SHELLS, if it exists.
- */
- char *
- getusershell(void)
- {
- char *ret;
-
- if (curshell == NULL)
- curshell = initshells();
- ret = *curshell;
- if (ret != NULL)
- curshell++;
- return (ret);
- }
-
- void
- endusershell(void)
- {
- if (shells != NULL)
- free((char *) shells);
- shells = NULL;
- if (strings != NULL)
- free(strings);
- strings = NULL;
- curshell = NULL;
- }
-
- void
- setusershell(void)
- {
- curshell = initshells();
- }
-
- static char **
- initshells(void)
- {
- register char **sp,
- *cp;
- register FILE *fp;
- struct stat statb;
-
- if (shells != NULL)
- free((char *) shells);
- shells = NULL;
- if (strings != NULL)
- free(strings);
- strings = NULL;
- if ((fp = fopen(SHELLS, "r")) == (FILE *) 0)
- return (okshells);
- if (fstat(fileno(fp), &statb) == -1) {
- (void) fclose(fp);
- return (okshells);
- }
- if ((strings = (char *) malloc((unsigned) statb.st_size + 1)) == NULL) {
- (void) fclose(fp);
- return (okshells);
- }
- shells = (char **) calloc((unsigned) statb.st_size / 3, sizeof(char *));
-
- if (shells == NULL) {
- (void) fclose(fp);
- free(strings);
- strings = NULL;
- return (okshells);
- }
- sp = shells;
- cp = strings;
- while (fgets(cp, MAXPATHLEN + 1, fp) != NULL) {
- #ifndef AMIGA
- while (*cp != '#' && *cp != '/' && *cp != '\0')
- cp++;
- #endif
- if (*cp == '#' || *cp == '\0')
- continue;
- *sp++ = cp;
- while (!isspace(*cp) && *cp != '#' && *cp != '\0')
- cp++;
- *cp++ = '\0';
- }
- *sp = (char *) 0;
- (void) fclose(fp);
- return (shells);
- }
-